package org.dromara.web.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.web.system.domain.AppSupplierDetail;
import org.dromara.web.system.domain.bo.AppSupplierDetailBo;
import org.dromara.web.system.domain.vo.AppSupplierDetailVo;
import org.dromara.web.system.mapper.AppSupplierDetailMapper;
import org.dromara.web.system.service.IAppSupplierDetailService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 供应商详情Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-19
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AppSupplierDetailServiceImpl implements IAppSupplierDetailService {

    private final AppSupplierDetailMapper baseMapper;

    /**
     * 查询供应商详情
     *
     * @param id 主键
     * @return 供应商详情
     */
    @Override
    public AppSupplierDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询供应商详情列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 供应商详情分页列表
     */
    @Override
    public TableDataInfo<AppSupplierDetailVo> queryPageList(AppSupplierDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppSupplierDetail> lqw = buildQueryWrapper(bo);
        Page<AppSupplierDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的供应商详情列表
     *
     * @param bo 查询条件
     * @return 供应商详情列表
     */
    @Override
    public List<AppSupplierDetailVo> queryList(AppSupplierDetailBo bo) {
        LambdaQueryWrapper<AppSupplierDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppSupplierDetail> buildQueryWrapper(AppSupplierDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppSupplierDetail> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(AppSupplierDetail::getId);
        lqw.like(StringUtils.isNotBlank(bo.getSupplierName()), AppSupplierDetail::getSupplierName, bo.getSupplierName());
        lqw.eq(bo.getAuditStatus() != null, AppSupplierDetail::getAuditStatus, bo.getAuditStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getSupplierCode()), AppSupplierDetail::getSupplierCode, bo.getSupplierCode());
        lqw.eq(StringUtils.isNotBlank(bo.getTaxpayerId()), AppSupplierDetail::getTaxpayerId, bo.getTaxpayerId());
        lqw.eq(StringUtils.isNotBlank(bo.getCompanyScale()), AppSupplierDetail::getCompanyScale, bo.getCompanyScale());
        lqw.like(StringUtils.isNotBlank(bo.getBankName()), AppSupplierDetail::getBankName, bo.getBankName());
        lqw.eq(StringUtils.isNotBlank(bo.getBankAccount()), AppSupplierDetail::getBankAccount, bo.getBankAccount());
        lqw.eq(bo.getHasVatInvoice() != null, AppSupplierDetail::getHasVatInvoice, bo.getHasVatInvoice());
        lqw.eq(bo.getVatRate() != null, AppSupplierDetail::getVatRate, bo.getVatRate());
        lqw.eq(StringUtils.isNotBlank(bo.getRegisteredAddress()), AppSupplierDetail::getRegisteredAddress, bo.getRegisteredAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getLegalRepresentative()), AppSupplierDetail::getLegalRepresentative, bo.getLegalRepresentative());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), AppSupplierDetail::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessInfo()), AppSupplierDetail::getBusinessInfo, bo.getBusinessInfo());
        lqw.eq(bo.getStickyLabel() != null, AppSupplierDetail::getStickyLabel, bo.getStickyLabel());
        lqw.eq(bo.getCorrugatedPaper() != null, AppSupplierDetail::getCorrugatedPaper, bo.getCorrugatedPaper());
        lqw.eq(bo.getBlueFilm() != null, AppSupplierDetail::getBlueFilm, bo.getBlueFilm());
        lqw.eq(bo.getSunflowerBag() != null, AppSupplierDetail::getSunflowerBag, bo.getSunflowerBag());
        lqw.eq(bo.getCoatedPaper() != null, AppSupplierDetail::getCoatedPaper, bo.getCoatedPaper());
        lqw.eq(bo.getOtherMaterial() != null, AppSupplierDetail::getOtherMaterial, bo.getOtherMaterial());
        return lqw;
    }

    /**
     * 新增供应商详情
     *
     * @param bo 供应商详情
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AppSupplierDetailBo bo) {
        AppSupplierDetail add = MapstructUtils.convert(bo, AppSupplierDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改供应商详情
     *
     * @param bo 供应商详情
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AppSupplierDetailBo bo) {
        AppSupplierDetail update = MapstructUtils.convert(bo, AppSupplierDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppSupplierDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除供应商详情信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public int updateStatus(String id, String status) {
        LambdaUpdateWrapper<AppSupplierDetail> appSupplierDetailLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        appSupplierDetailLambdaUpdateWrapper.eq(AppSupplierDetail::getId, id);
        appSupplierDetailLambdaUpdateWrapper.set(AppSupplierDetail::getAuditStatus, status);
        return baseMapper.update(appSupplierDetailLambdaUpdateWrapper);
    }
}
